package cloud.provider.payment.controller;

import cn.hutool.core.util.IdUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @auther xfanonymous
 * @create 2023-12-26 22:26
 *
 * 1. Resilience4J CircuitBreaker 断路器，验证异常、超时、正常场景
 * 2. Resilience4J BulkHead
 */
@RestController
public class PayCircuitController
{
    // Resilience4j CircuitBreaker
    @GetMapping(value = "/pay/circuit/{id}")
    public String myCircuit(@PathVariable("id") Integer id)
    {
        // 验证请求次数配置
        if(id == -4) throw new RuntimeException("----circuit id 不能负数");

        // 验证请求时间配置
        if(id == 9999)
        {
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        return "Hello, circuit! inputId:  "+id+" \t " + IdUtil.simpleUUID();
        // Hello, circuit! inputId:  32 	 c1c5d9df184440c3b39cabd07557fa9c
    }

    // Resilience4J BulkHead
    @GetMapping(value = "/pay/bulkhead/{id}")
    public String myBulkhead(@PathVariable("id") Integer id)
    {
        if(id == -4) throw new RuntimeException("----bulkhead id 不能-4");

        if(id == 9999)
        {
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        return "Hello, bulkhead! inputId:  "+id+" \t " + IdUtil.simpleUUID();
    }


    //Resilience4J RateLimit
    @GetMapping(value = "/pay/ratelimit/{id}")
    public String myRateLimit(@PathVariable("id") Integer id)
    {
        return "Hello, myRateLimit inputId:  "+id+" \t " + IdUtil.simpleUUID();
    }
}
